package com.linzhaowei.headhuntingservice;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.linzhaowei.headhuntingservice.bean.JobObjective;
import com.linzhaowei.headhuntingservice.utils.HttpUtils;
import com.linzhaowei.headhuntingservice.utils.Ip;

import java.io.IOException;

public class JobObjectiveUpdate extends AppCompatActivity implements View.OnClickListener{
    JobObjective jobObjective;

    EditText name;
    EditText address;
    EditText salary;
    Button button;
    TextView del;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job_objective_update);
        jobObjective=(JobObjective) getIntent().getSerializableExtra("jobObjective");
        initViews();
        setEvents();
        setJobObjective();
        del.setVisibility(View.VISIBLE);
    }



    private void initViews(){
        name=findViewById(R.id.j_o_u_name);
        address=findViewById(R.id.j_o_u_address);
        salary=findViewById(R.id.j_o_u_salary);
        button=findViewById(R.id.j_o_u_btn);
        del=findViewById(R.id.j_o_u_del);
    }

    private void setEvents(){
        button.setOnClickListener(this);
        del.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.j_o_u_btn:
                update();
                break;
            case R.id.j_o_u_del:
                del();
                break;
                default:
                    break;
        }
    }

    void setJobObjective(){
        name.setText(jobObjective.getName());
        address.setText(jobObjective.getAddress());
        salary.setText(jobObjective.getSalary()+"");
    }

    void update(){
        SharedPreferences sharedPreferences=getSharedPreferences("user", Context.MODE_PRIVATE);
        jobObjective.setUser_id(sharedPreferences.getInt("id",-1));
        jobObjective.setName(name.getText().toString().trim());
        jobObjective.setAddress(address.getText().toString().trim());
        jobObjective.setSalary(Integer.valueOf(salary.getText().toString()));
        String string= JSON.toJSONString(jobObjective);
        new Thread(()->{
            HttpUtils httpUtils=new HttpUtils();
            try{
                httpUtils.post(Ip.ip+"/user/update_job_objective",string);
                runOnUiThread(()->{
                    Toast.makeText(JobObjectiveUpdate.this,"修改成功",Toast.LENGTH_SHORT).show();
                });
                finish();
            }catch (IOException e){
                e.printStackTrace();
            }
        }).start();
    }

    void del(){
        SharedPreferences sharedPreferences=getSharedPreferences("user", Context.MODE_PRIVATE);
        jobObjective.setUser_id(sharedPreferences.getInt("id",-1));
        String string= JSON.toJSONString(jobObjective);
        new Thread(()->{
            HttpUtils httpUtils=new HttpUtils();
            try{
                httpUtils.post(Ip.ip+"/user/del_job_objective",string);
                runOnUiThread(()->{
                    Toast.makeText(JobObjectiveUpdate.this,"删除成功",Toast.LENGTH_SHORT).show();
                });
                finish();
            }catch (IOException e){
                e.printStackTrace();
            }
        }).start();
    }
}
